<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addDialogVisible = true"
          >添加分类</el-button
        >
      </div>
      <!-- 文章列表 -->
      <el-table style="width: 100%" :data="artCateList" border>
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column label="分类名称">
          <template v-slot="{ row }">
            {{row.cate_name}}
          </template>
        </el-table-column>
        <el-table-column label="分类别名">
          <template v-slot="{ row }">
            {{row.cate_alias}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
            type="primary"
            size="mini"
            @click="showEditDialog(row)"
            >修改
            </el-button>
            <el-button type="danger" size="mini" @click="deleteArtCate(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加文章分类弹出框 -->
    <el-dialog
      title="修改文章分类"
      :visible.sync="addDialogVisible"
      width="60%"
      @closed="$refs.addFormRef.resetFields()"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addArtCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改文章分类框 -->
    <el-dialog
      title="修改文章分类"
      :visible.sync="editDialogVisible"
      width="60%"
      @closed="$refs.editFormRef.resetFields()"
    >
      <el-form
        :model="editForm"
        :rules="addFormRules"
        ref="editFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editArtCate">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "ArtCate",
  data() {
    return {
      // 存储文章分类列表
      artCateList: [],
      // 添加文章分类弹出框默认隐藏
      addDialogVisible: false,
      // 修改文章分类弹出框
      editDialogVisible: false,
      // 添加表单的数据对象
      addForm: { cate_name: "", cate_alias: "" },
      // 添加表单的验证规则对象
      addFormRules: {
        cate_name: [
          {
            required: true,
            message: "请输入分类名称",
            trigger: "blur",
          },
          {
            pattern: /^\S{1,10}$/,
            message: "分类名必须是1-10位的非空字符",
            trigger: "blur",
          },
        ],
        cate_alias: [
          { required: true, message: "请输入分类别名", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: "分类别名必须是1-15位的字母数字",
            trigger: "blur",
          },
        ],
      },
      // 修改文章分类数据对象
      editForm: { cate_name: "", cate_alias: "" },
    };
  },
  methods: {
    // 获取文章分类函数
    async initCateList() {
      const { data: res } = await this.$http.get("/my/cate/list");
      if (res.code === 0) {
        this.artCateList = res.data;
        console.log(res, "123");
      }
    },
    // 添加文章分类
    addArtCate() {
      // 兜底校验
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return; //效验失败
        // 校验成功 发送添加文章分类请求
        const { data: res } = await this.$http.post(
          "/my/cate/add",
          this.addForm
        );
        if (res.code !== 0) return this.$message.error(res.message);
        this.$message.success(res.message);
        // 关闭对话框
        (this.addDialogVisible = false),
          // 重新获取文章分类数据
          this.initCateList();
      });
    },
    /* // 方法一 : 点击修改按钮获取数据填充到表单
    async showEditDialog(id) {
      const{data:res} = await this.$http.get('/my/cate/info',{
        params:{id}
      })
      if(res.code ===0){
        // 为data中的editForm修改表单赋值
        this.editForm = res.data
        // 显示弹框
        this.editDialogVisible = true;
      }
    }, */
    // 方法二: 直接将获取的row赋值给更改弹框表单
    showEditDialog(obj){
      // 汉考运算符
      this.editForm = {...obj}
      // 显示弹框
      this.editDialogVisible = true
    },
    // 点击修改弹出框确定按钮实现修改文章分类
    editArtCate(){
      this.$refs.editFormRef.validate(async valid=>{
        if(!valid)return
        const {data:res} =await this.$http.put('/my/cate/info',this.editForm)
        if(res.code!==0)return this.$message.error(res.message)
        this.$message.success(res.message)
        // 关闭弹出框
        this.editDialogVisible = false
        // 重新渲染文章列表
        this.initCateList()
      })
    },
    // 删除按钮删除文章分类
    deleteArtCate(id) {
        this.$confirm('确定要删除该文章分类吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          // 确认 发送请求
         const{data:res}=await this.$http.delete('/my/cate/del',{
            params:{
              id
            }
          })
          if(res.code!==0)return this.$message.error(res.message)
          this.$message.success(res.message)
          // 重新渲染数据
          this.initCateList()
        }).catch(() => {
          // 取消
        });
      }
  },
  created() {
    this.initCateList();
  },
};
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
